package com.wangjiang.mario.admin.system.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wangjiang.mario.admin.common.base.BaseServiceImpl;
import com.wangjiang.mario.admin.system.domain.entity.SystemMenuEntity;
import com.wangjiang.mario.admin.system.domain.mapper.SystemMenuMapper;
import com.wangjiang.mario.admin.system.domain.service.SystemMenuService;
import com.wangjiang.mario.admin.system.domain.util.ShiroUtils;
import com.wangjiang.mario.admin.system.object.user.UserRoleMenuVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemMenuServiceImpl extends BaseServiceImpl<SystemMenuMapper, SystemMenuEntity>
        implements SystemMenuService {

    private static final Logger logger = LoggerFactory.getLogger(SystemMenuServiceImpl.class);

    /**
     * 当前用户的获取所有的顶级菜单
     * @return
     */
    @Override
    public List<SystemMenuEntity> getTopMenuByCurrentUser() {
        UserRoleMenuVo userRoleMenuVo = ShiroUtils.getCurrentUser();
        if(userRoleMenuVo.isAdmin()) {
           return getTopMenu();
        } else {
            return baseMapper.getTopMenuByUserUUid(userRoleMenuVo.getUuid());
        }
    }

    private List<SystemMenuEntity> getTopMenu() {
        QueryWrapper<SystemMenuEntity> queryWrapper = getDefaultWrapper();
        queryWrapper.eq("tree_level", 0);
        queryWrapper.orderBy(true, true, "tree_sort");

        return baseMapper.selectList(queryWrapper);
    }

    // @Autowired
    // private JwtService jwtService;

}
